package com.llb.llbliving.commodity.controller;

import com.aliyun.oss.OSSClient;
import com.llb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@RestController
@Slf4j
public class TestController {

    //编写一个方法上传指定的文件到指定bucket
//    @RequestMapping("/test")
//    public R testUpload() throws FileNotFoundException {
//        String endpoint = "oss-cn-beijing.aliyuncs.com";
//        String accessKeyId = "LTAI5tMoJznbXPwbuJWiokTH";
//        String accessKeySecret = "5yR0j25rUOauwLHSnMP3TvqsQblxTk";
//        String bucketName="llbliving-10000";
//        String objectName="4.jpg";
//        String filePath="C:\\Users\\12967\\Pictures\\Saved Pictures\\4.jpg";
//        OSS ossClient=new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
//        try {
//            InputStream inputStream = new FileInputStream(filePath);
//            // 创建PutObjectRequest对象。
//            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
//            // 创建PutObject请求。
//            PutObjectResult result = ossClient.putObject(putObjectRequest);
//        } catch (OSSException oe) {
//            System.out.println("Caught an OSSException, which means your request made it to OSS, "
//                    + "but was rejected with an error response for some reason.");
//            System.out.println("Error Message:" + oe.getErrorMessage());
//            System.out.println("Error Code:" + oe.getErrorCode());
//            System.out.println("Request ID:" + oe.getRequestId());
//            System.out.println("Host ID:" + oe.getHostId());
//        } catch (ClientException ce) {
//            System.out.println("Caught an ClientException, which means the client encountered "
//                    + "a serious internal problem while trying to communicate with OSS, "
//                    + "such as not being able to access the network.");
//            System.out.println("Error Message:" + ce.getMessage());
//        } finally {
//            if (ossClient != null) {
//                ossClient.shutdown();
//            }
//        }
//        return null;
//    }

    //装配OSSClient
    @Resource
    private OSSClient ossClient;
    //上传指定的文件到bucket
    @RequestMapping("/test2")
    public R testUpload2() throws FileNotFoundException {
        InputStream inputStream =
                new FileInputStream("C:\\Users\\12967\\Pictures\\Saved Pictures\\3.png");
        ossClient.putObject("llbliving-10000","4.png",inputStream);
        ossClient.shutdown();
        return R.ok("上传ok");
    }

}
